Imports System.ComponentModel

Friend Class frmOptions
    Private _Zed As ZedGraph.ZedGraphControl
    Private _Axis As clsAxis
    Private _DefaultAxis As Integer = 1
    Private _DefaultIndex As Integer = 0
    Private GraphOptionsChanged As Boolean = False
    Private SeriesDefaultsChanged As Boolean = False

    Public Sub New()
        InitializeComponent()
        _Zed = ActiveGraphTab.Zed
        _GraphType = ActiveGraphTab.GraphType 'set global variable needed by typeconverter
    End Sub

    ''' <summary>
    ''' Called in response to pressing of Reset button; resets all graph settings to their default format (either user defaults or program defaults)
    ''' </summary>
    Private Sub Reset(ByVal UserDefaults As Boolean)
        Dim DefaultFile As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\Default.graph"
        If UserDefaults Then 'use existing default settings file
            GraphForm.LoadSettings()
        Else 'delete default settings file and reset to program defaults
            If My.Computer.FileSystem.FileExists(DefaultFile) Then My.Computer.FileSystem.DeleteFile(DefaultFile)
            GraphForm.LoadSettings()
        End If
        pgAxes.Refresh()
        pgGraph.Refresh()
    End Sub

    Private Sub cboAxis_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAxis.SelectedIndexChanged
        With cboAxisIndex
            .Items.Clear()
            Select Case cboAxis.SelectedIndex
                Case 0 : .Items.Add("1") : clsGraphTab.CurrentAxisType = enumAxisType.XAxis 'set to ConvAxisType will offer up appropriate axis types
                Case 1, 2 : .Items.AddRange(New String() {"1", "2", "3", "4", "5"}) : clsGraphTab.CurrentAxisType = enumAxisType.YAxis
            End Select
            .SelectedIndex = 0
        End With
    End Sub

    Private Sub cboAxisIndex_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAxisIndex.SelectedIndexChanged
        Dim AxisType As clsAxis.enumAxis
        Select Case cboAxis.SelectedIndex
            Case 0
                AxisType = clsAxis.enumAxis.XAxis
            Case 1
                AxisType = clsAxis.enumAxis.YAxis
            Case 2
                AxisType = clsAxis.enumAxis.Y2Axis
        End Select
        _Axis = New clsAxis(_Zed, AxisType, cboAxisIndex.SelectedIndex, ActiveGraphTab.GraphOptions.FontScaleFactor)
        pgAxes.SelectedObject = _Axis
        _Zed.AxisChange()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If SeriesDefaultsChanged AndAlso WarningMsg("You have changed one or more 'Series Line and Symbol Default Formats'; these will not be used unless you reset your defaults. Do you want to do so now?") = Windows.Forms.DialogResult.OK Then
            btnDefaults.PerformClick()
        End If
        SaveSetting(REGAPP, "General", "AutoLoad", chkAutoLoad.Checked)
        SaveSetting(REGAPP, "General", "BMDMemory", chkBMDMemory.Checked)
        SaveSetting(REGAPP, "General", "ConfirmTables", chkConfirmTables.Checked)
        SaveSetting(REGAPP, "General", "RefreshParadox", chkRefreshParadox.Checked)
        SaveSetting(REGAPP, "General", "SuppressSavePrompt", chkSuppressSavePrompt.Checked)

        ExpertMode = chkExpertMode.Checked
        SaveSetting(REGAPP, "General", "ExpertMode", ExpertMode)
        SaveSetting(REGAPP, "General", "BaseDate", dtBaseDate.Value.ToString(System.Globalization.CultureInfo.InvariantCulture))
        SaveSetting(REGAPP, "General", "TimeOffset", CInt(Val(txtTimeOffset.Text)))
        If ApplyToAllTabs AndAlso GraphForm.TabGraph.TabCount > 2 Then ApplyToAll()
        Close()
    End Sub

    Private Sub ApplyToAll()
        If GraphOptionsChanged AndAlso (ExpertMode OrElse WarningMsg("Are you sure you want to copy these graph format settings to all graph tabs?") = Windows.Forms.DialogResult.OK) Then
            Dim g As clsGraphOptions = ActiveGraphTab.GraphOptions
            For i As Integer = 0 To GraphForm.TabGraph.TabCount - 2
                With ActiveGraphTab(i).GraphOptions
                    '.BaseDimension = g.BaseDimension
                    .FillColor = g.FillColor
                    '.GraphIsScaled = g.GraphIsScaled
                    .LegendBorder = g.LegendBorder
                    .LegendFont = g.LegendFont
                    .LegendPos = g.LegendPos
                    .LegendVisible = g.LegendVisible
                    .NotePosition = g.NotePosition
                    .NoteText = g.NoteText
                    .ScrollBars = g.ScrollBars
                    .ShowPoints = g.ShowPoints
                    If g.AutoGraphTitle Then
                        .AutoGraphTitle = True
                        ActiveGraphTab(i).SetGraphTitle()
                    Else
                        .GraphTitle = g.GraphTitle
                    End If
                    .TitleFont = g.TitleFont
                    '.YAxesSynced = g.YAxesSynced
                End With
            Next
        End If
    End Sub

    Private Sub frmOptions_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveWindowPos(REGAPP, Me)
    End Sub

    'following two are so can set defaults in comboboxes when which on graph axis

    Friend WriteOnly Property DefaultAxis() As Integer
        Set(ByVal value As Integer)
            _DefaultAxis = value
        End Set
    End Property

    Friend WriteOnly Property DefaultIndex() As Integer
        Set(ByVal value As Integer)
            _DefaultIndex = value
        End Set
    End Property

    Private Sub frmOptions_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetWindowPos(REGAPP, Me)
        cboAxis.SelectedIndex = _DefaultAxis
        cboAxisIndex.SelectedIndex = _DefaultIndex
        pgGraph.SelectedObject = ActiveGraphTab.GraphOptions
        _Zed.GraphPane.AxisChange()
        _Zed.Refresh()
        chkAutoLoad.Checked = GetSetting(REGAPP, "General", "AutoLoad", "False")
        chkExpertMode.Checked = GetSetting(REGAPP, "General", "ExpertMode", "False")
        chkConfirmTables.Checked = GetSetting(REGAPP, "General", "ConfirmTables", "False")
        chkRefreshParadox.Checked = GetSetting(REGAPP, "General", "RefreshParadox", "False")
        chkSuppressSavePrompt.Checked = GetSetting(REGAPP, "General", "SuppressSavePrompt", "True")

        chkBMDMemory.Checked = GetSetting(REGAPP, "General", "BMDMemory", "False")
        dtBaseDate.Value = Date.Parse(GetSetting(REGAPP, "General", "BaseDate", "1/1/1901"), System.Globalization.CultureInfo.InvariantCulture)
        txtTimeOffset.Text = GetSetting(REGAPP, "General", "TimeOffset", "0")
        lblApply.Text = "Note: These settings will be applied to " & IIf(ApplyToAllTabs, "all graph tabs", "current tab only")
        lblApply.Visible = ApplyToAllTabs And GraphForm.TabGraph.TabCount > 2
        lnkAllTabs.Visible = Not ApplyToAllTabs And GraphForm.TabGraph.TabCount > 2
    End Sub

    Private Sub pg_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles pgGraph.PropertyValueChanged, pgAxes.PropertyValueChanged
        GraphOptionsChanged = True
        pgAxes.Refresh()
        pgGraph.Refresh()
        _Zed.GraphPane.AxisChange()
        _Zed.Refresh()
        GraphForm._IsModified = True
        If e.ChangedItem.Parent.Label = "Series Line and Symbol Default Formats" Then SeriesDefaultsChanged = True
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Reset(True)
        chkAutoLoad.Checked = False
        chkExpertMode.Checked = False
        chkBMDMemory.Checked = False
        dtBaseDate.Value = New Date(1901, 1, 1)
        txtTimeOffset.Text = 0
    End Sub

    Private Sub mnuReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuResetUser.Click, mnuResetProgram.Click
        Reset(sender Is mnuResetUser)
        chkAutoLoad.Checked = False
        chkExpertMode.Checked = False
        chkBMDMemory.Checked = False
        dtBaseDate.Value = New Date(1901, 1, 1)
        txtTimeOffset.Text = 0
    End Sub

    Private Sub SaveAxis(ByVal Section As String, ByVal Axis As ZedGraph.Axis)
        With Axis
            SaveSetting(REGAPP, Section & " Type " & _GraphType, "Title", .Title.Text)
            SaveSetting(REGAPP, Section & " Type " & _GraphType, "Type", .Type)
            SaveSetting(REGAPP, Section & " Type " & _GraphType, "ScaleReverse", .Scale.IsReverse)
            SaveSetting(REGAPP, Section & " Type " & _GraphType, "ScaleFormat", .Scale.Format.Replace(vbCrLf, "\n"))
            SaveFont(Section & " ScaleFont", .Scale.FontSpec)
            SaveFont(Section & " TitleFont", .Title.FontSpec)
        End With
    End Sub

    Private Sub SaveFont(ByVal Section As String, ByVal FontSpec As ZedGraph.FontSpec)
        With FontSpec
            SaveSetting(REGAPP, Section, "Family", .Family)
            SaveSetting(REGAPP, Section, "Size", .Size)
            SaveSetting(REGAPP, Section, "Color", .FontColor.ToString)
            SaveSetting(REGAPP, Section, "IsBold", .IsBold)
            SaveSetting(REGAPP, Section, "IsItalic", .IsItalic)
            SaveSetting(REGAPP, Section, "IsUnderline", .IsUnderline)
            SaveSetting(REGAPP, Section, "Alignment", .StringAlignment)
        End With
    End Sub

    ''' <summary>
    ''' Save current settings as new user defaults
    ''' </summary>
    Private Sub btnDefaults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefaults.Click
        Dim DefaultFile As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\Default.graph"
        Dim SaveSeriesDefaults As Boolean = False
        If ActiveGraphTab.GraphType = enumGraphType.TimeSeries Then
            With New frmSaveDefaults
                .ShowDialog(Me)
                Select Case .SaveAction
                    Case frmSaveDefaults.enumResult.Cancel : Exit Sub
                    Case frmSaveDefaults.enumResult.Graph : SaveSeriesDefaults = False
                    Case frmSaveDefaults.enumResult.Series : SaveSeriesDefaults = True
                End Select
            End With
        End If
        GraphForm.SaveSettings(DefaultFile, True, SaveSeriesDefaults)
        With ActiveGraphTab.GraphOptions
            SaveSetting(REGAPP, "GraphOptions", "ZoomOption", .ZoomOption)
            SaveSetting(REGAPP, "GraphOptions", "ShowPoints", .ShowPoints)
            SaveSetting(REGAPP, "GraphOptions", "Scrollbars", .ScrollBars)
        End With
        SeriesDefaultsChanged = False
    End Sub

    Private Sub lnkAllTabs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAllTabs.Click
        ApplyToAll()
    End Sub

    Private Sub lnkClearRecent_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkClearRecent.LinkClicked
        With GraphForm
            With .mru
                .RemoveAll()
                .ClearRegistry()
            End With
        End With
        frmInfoMsg.ResetAllInfoMsgs()
    End Sub
End Class

''' <summary>
''' This class created to allow selection of axis type from drop-down box in properties grid depending on the graph type
''' </summary>
Friend Class ConvAxisType : Inherits StringConverter
    Public Overloads Overrides Function GetStandardValues(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
        Dim colAxisType As New Generic.List(Of clsAxis.enumScaleType)
        If clsGraphTab.CurrentAxisType = enumAxisType.XAxis Then
            Select Case _GraphType
                Case enumGraphType.TimeSeries
                    colAxisType.Add(clsAxis.enumScaleType.Date)
                    colAxisType.Add(clsAxis.enumScaleType.Date_Adaptive)
                    colAxisType.Add(clsAxis.enumScaleType.Date_Julian)
                Case enumGraphType.Depth, enumGraphType.Scatter
                    colAxisType.Add(clsAxis.enumScaleType.Linear)
                    colAxisType.Add(clsAxis.enumScaleType.Log)
                Case enumGraphType.Profile, enumGraphType.Width, enumGraphType.Histogram
                    colAxisType.Add(clsAxis.enumScaleType.Linear)
                Case enumGraphType.Probability
                    colAxisType.Add(clsAxis.enumScaleType.Probability)
                Case enumGraphType.FlowDuration
                    colAxisType.Add(clsAxis.enumScaleType.Linear)
            End Select
        Else
            Select Case _GraphType
                Case enumGraphType.Profile, enumGraphType.TimeSeries, enumGraphType.Scatter, enumGraphType.Width, enumGraphType.Probability, enumGraphType.FlowDuration
                    colAxisType.Add(clsAxis.enumScaleType.Linear)
                    colAxisType.Add(clsAxis.enumScaleType.Log)
                Case enumGraphType.Depth, enumGraphType.Histogram
                    colAxisType.Add(clsAxis.enumScaleType.Linear)
            End Select
        End If
        Return New StandardValuesCollection(colAxisType)
    End Function

    Public Overloads Overrides Function GetStandardValuesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
        Return True
    End Function

    Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
        If sourceType Is GetType(String) Then
            Return True
        Else
            Return MyBase.CanConvertFrom(context, sourceType)
        End If
    End Function

    Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
        If value.GetType() Is GetType(String) Then
            ' Parses the string to get the value to set to the property.
            Dim ScaleType As clsAxis.enumScaleType
            If [Enum].TryParse(value, ScaleType) Then
                Return ScaleType
            Else
                Return clsAxis.enumScaleType.Linear
            End If
        Else
            Return MyBase.ConvertFrom(context, culture, value)
        End If
    End Function 'ConvertFrom

End Class


